<template>
  <div>
    <tableComponents
      :isHaveExport="false"
      :isShow="false"
      :isHavePagination="false"
      @toAdd="add_limit_btn"
    >
      <template v-slot:table>
        <el-table
          ref="multipleTable"
          :data="limit_list"
          v-loading="loading"
          stripe
          header-row-class-name="head_color"
          tooltip-effect="dark"
          style="width: 99.8%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column label="活动开始时间" min-width="200">
            <template slot-scope="scope">{{scope.row.begin_time |getTimeFormat}}</template>
          </el-table-column>
          <el-table-column label="活动结束时间" min-width="200">
            <template slot-scope="scope">{{scope.row.end_time |getTimeFormat}}</template>
          </el-table-column>
          <el-table-column label="操作" min-width="80">
            <template slot-scope="scope">
              <el-button type="danger" size="mini" @click="del_limit_data(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <template v-slot:footerLeft>
        <a class="aBtn aBtn_1 aBtn_lan2" @click="deleteAll_robots">删除</a>
      </template>
    </tableComponents>

    <el-dialog
      :close-on-click-modal="false"
      title="添加活动时间"
      v-loading="dioLoading"
      class="tl size_13"
      width="800px"
      :visible.sync="dialogaddVisible"
    >
      <el-form ref="form" label-width="120px">
        <el-form-item label="活动时间">
          <el-col :span="12">
            <el-date-picker
              style="width:100%"
              v-model="createTime"
              type="datetimerange"
              size="small"
              align="right"
              unlink-panels
              value-format="timestamp"
              range-separator="-"
              clearable
              start-placeholder="活动时间起"
              end-placeholder="活动时间止"
            ></el-date-picker>
          </el-col>
        </el-form-item>

        <el-form-item>
          <el-button :loading="$store.state.isLoading" type="primary" @click="add_submit()">添 加</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>

export default {
  name: "limit_set",
  data() {
    return {
      multipleSelection: [],
      add_limit: {
        created_time_begin: "",
        created_time_end: ""
      },
      createTime: [],
      dialogaddVisible: false,
      dioLoading: false,
      loading: false,
      limit_list: [],
      currentPage: 1, //初始页
      pagesize: 10 //    每页的数据
    };
  },

  created() {
    this.get_limit_list(1);
  },
  methods: {
    del_limit_data(id) {
      //删除
      this.$swal({
        title: "您确定要删除这些信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是的，我要删除",
        cancelButtonText: "容我三思"
      }).then(willDelete => {
        if (willDelete.value) {
          this.$api.getShops.del_limittime({ id_str: id }).then(res => {
            if (res.data.code == 200) {
              this.$swal("删除成功！", "您已经删除了这条信息。", "success");
              this.get_limit_list(1);
            } else {
              this.$swal("删除失败！", res.data.result, "error");
              this.get_limit_list(1);
            }
          });
        }
      });
    },
    deleteAll_robots() {
      //批量删除

      var str = "";
      if (this.multipleSelection.length <= 0) {
        this.$message.error("未选择批量删除");
      } else {
        for (var i in this.multipleSelection) {
          str += this.multipleSelection[i].id + "@";
        }
        str = str.slice(0, -1);
        this.$swal({
          title: "您确定要删除这些信息吗",
          text: "删除后将无法恢复，请谨慎操作！",
          type: "warning",
          showCancelButton: true,
          confirmButtonColor: "#DD6B55",
          confirmButtonText: "是的，我要删除",
          cancelButtonText: "容我三思"
        }).then(willDelete => {
          if (willDelete.value) {
            this.$api.getShops.del_limittime({ id_str: str }).then(res => {
              if (res.data.code == 200) {
                this.$swal("删除成功！", "您已经删除了这条信息。", "success");
                this.get_limit_list(1);
              } else {
                this.$swal("删除失败！", res.data.result, "error");
                this.get_limit_list();
              }
            });
          }
        });
      }
    },

    handleSelectionChange(val) {
      this.multipleSelection = val;
    },

    add_submit() {
      var params = {};
      params.begin_time = this.add_limit.created_time_begin;
      params.end_time = this.add_limit.created_time_end;
      this.$api.getShops.add_limittime(params).then(res => {
        if (res.data.code == 200) {
          this.$fnc.alertSuccess("添加成功");
          this.dioLoading = false;
          this.dialogaddVisible = false;
          this.get_limit_list(1);
        } else {
          this.$fnc.alertError(res.data.result);
          this.loading = false;
        }
      });
    },
    add_limit_btn() {
      this.createTime = [];
      this.dialogaddVisible = true;
    },
    get_limit_list(page) {
      this.loading = true;
      this.$api.getShops
        .get_limittime({ page: page, page_size: this.pagesize })
        .then(res => {
          if (res.data.code == 200) {
            this.limit_list = res.data.result;
            this.loading = false;
            this.dialogaddVisible = false;
          } else {
            this.$fnc.alerterror(res.data.result);
            this.loading = false;
          }
        });
    }
  },
  watch: {
    createTime(val) {
      if (val != null) {
        this.add_limit.created_time_begin = this.$fnc.phptime(val[0]);
        this.add_limit.created_time_end = this.$fnc.phptime(val[1]);
      } else {
        this.add_limit.created_time_begin = "";
        this.add_limit.created_time_end = "";
      }
    }
  }
};
</script>
<style  scoped>
</style>
